package com.mytomcat.catalina.adapter;

import com.mytomcat.catalina.connector.Connector;
import com.mytomcat.catalina.request.Request;
import com.mytomcat.catalina.response.Response;
import java.net.Socket;

/**
 * 适配器实现类
 *
 * @author supu
 * @date 2019-04-22 16:29
 **/
public class CoyoteAdapter implements Adapter{
    private Connector connector;

    public CoyoteAdapter(Connector connector) {
        this.connector = connector;
    }

    @Override
    public void service(Socket socket) {
        try {
            // socket 构建request
            Request request = new Request(socket.getInputStream());
            request.setContainer(connector.getService().getContainer());

            // socket 构建response
            Response response = new Response(socket.getOutputStream());

            connector.getService().getContainer().getPipeline().getFirst().invoke(request,response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
